from plane_sprites import *
from tools.tools import *


class PlaneGame:

    def __init__(self):
        pygame.init()

        # 设置icon图标
        icon = pygame.image.load("icon.png")
        pygame.display.set_icon(icon)

        # 设置游戏标题
        pygame.display.set_caption(GAME_TITLE)

        # 初始化游戏窗口大小
        self.screen = pygame.display.set_mode(SCREEN_RECT.size)

        # 设置游戏时钟
        self.clock = pygame.time.Clock()

        # 创建相关的精灵
        self.__create_sprites()

        # 设置创建敌机的事件，每1s触发一次
        pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)

        # 子弹发射间隔
        pygame.time.set_timer(HERO_FIRE_EVENT, 37)

    def start_game(self):
        while True:
            self.clock.tick(FRAME_PER_SEC)
            # 1.事件处理
            self.__event_handler()
            # 2.碰撞检测
            self.__check_collide()
            # 3. 位置更新
            self.__update_sprites()
            # 4. 屏幕刷新
            pygame.display.update()

    def __create_sprites(self):
        """精灵创建类"""

        # 背景图片，4张图片拼接滚动实现
        background1 = Background()
        background2 = Background()
        background3 = Background()
        background4 = Background()

        # 靠右的两张图片进行镜像反转，延y轴翻转
        background3.image = pygame.transform.flip(background3.image, True, False)
        background4.image = pygame.transform.flip(background4.image, True, False)

        # 设置图片的初始位置
        background1.rect.y = -background1.rect.height + SCREEN_RECT.height
        background2.rect.y = -2 * background1.rect.height + SCREEN_RECT.height
        background3.rect.y = -background1.rect.height + SCREEN_RECT.height
        background4.rect.y = -2 * background1.rect.height + SCREEN_RECT.height
        background3.rect.x = background3.rect.width
        background4.rect.x = background4.rect.width
        self.background_group = pygame.sprite.Group(background1, background2, background3, background4)

        # 创建玩家图片
        self.player = Player("Fly_Player1")
        self.player_group = pygame.sprite.Group(self.player)

    def __event_handler(self):
        """事件处理类"""

        for event in pygame.event.get():
            # 判断是否退出，即右上角的小叉叉
            if event.type == pygame.QUIT:
                PlaneGame.__game_over()
            # 判断是否创建敌机事件触发
            elif event.type == CREATE_ENEMY_EVENT:
                print("创建敌机")
            elif event.type == HERO_FIRE_EVENT:
                # 子弹发射
                self.player.fire()

        # 出生并到达指定位置后开始事件检测
        if bool(1 - self.player.isUnbeatable):
            # 按键检测
            keydown_flag = True
            keys_pressed = pygame.key.get_pressed()
            # 检测是否按下右，左，上，下建
            if keys_pressed[pygame.K_RIGHT]:
                self.player.move_right()
                keydown_flag = False
            if keys_pressed[pygame.K_LEFT]:
                self.player.move_left()
                keydown_flag = False
            if keys_pressed[pygame.K_UP]:
                self.player.move_up()
                keydown_flag = False
            if keys_pressed[pygame.K_DOWN]:
                self.player.move_down()
                keydown_flag = False
            if keydown_flag:
                self.player.not_move()
        else:
            self.player.born()

    def __check_collide(self):
        """检测碰撞类"""
        pass

    def __update_sprites(self):
        """图形更新类"""
        self.background_group.update()
        self.background_group.draw(self.screen)
        self.player_group.update()
        self.player_group.draw(self.screen)

        self.player.bullets.update()
        self.player.bullets.draw(self.screen)

    @staticmethod
    def __game_over():
        """封装游戏推出静态方法"""
        pygame.quit()
        exit()


if __name__ == '__main__':
    game = PlaneGame()
    game.start_game()
